import formJs from "./regexDetail.js"; //新增弹框配置项
// import publicForm from "@/components/public/up-form.vue"; //弹框form
import publicForm from "@/components/public/el-form.vue"; //弹框form


import {
    getBeam, //详情
    saveBeam, //保存
} from "@/api/BackStage/stationProductionPage/request.js";

export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
    ],
    data() {
        return {
            visible: "",
            title: null,
            isLock: false,
            dialogHeight: null,
            formObj: formJs.formObj, //基础信息
            formData: {
                fId: "", //传fid表示修改 不传表示新增
                fCompanyid: "", //标段id
                fCompanyname: "", //标段名称
                fProjectid: "", //项目id
                fProjectname: "", //项目名称
                fBridgename: "",// 桥梁名称
                fCenternumber: "",// 中心桩号
                fHolespan: "",// 孔跨
                fAngle: "",// 交角
                fBridgelength: "",// 桥长
                fBeamlocation: "",//   梁板位置  
                fBeamtype: "",// 梁板种类
                fBeamspan: "",//   跨径  
                fBeamcount: "",// 梁板数量
                fMin: "",//已经预制数
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
            },
            btnShow: true,
            newRow: null,

        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }

            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.newRow = newVal;
                }
            },

            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },

    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        console.log("userInfo", this.userInfo);
        this.btnShow = true;
        this.formObj.map((item) => {
            item.disabled = false;
        });
        this.formObj[0].disabled = true;
        this.formObj[1].disabled = true;
        this.formObj[11].disabled = true;
        if (this.statusNum == 1) { //新增
            this.formData.fCompanyid = this.userInfo.companyId;//标段id
            this.formData.fCompanyname = this.userInfo.companyName; //标段名称
            this.formData.fProjectid = this.userInfo.projectId;//项目id
            this.formData.fProjectname = this.userInfo.projectName; //项目名称
            this.formData.fMin = "0"; //已经预制数
        } else if (this.statusNum == 2) { //编辑
            this.getDetail({
                fId: this.newRow.fId
            })
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false;
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.getDetail({
                fId: this.newRow.fId
            })
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },

        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            // console.log("selectChange", data, a, b);
            if (b == "fBeamlocation") {
                this.formData.fBeamlocation = data.label;
            }
            if (b == "fBeamtype") {
                this.formData.fBeamtype = data.label;
            }
        },

        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        // 详情
        getDetail(obj) {
            var that = this
            getBeam(obj).then((res) => {
                if (res.errcode == 0) {
                    that.formData = res.data.precastBeam
                }
            });
        },
        // 保存
        yj_saveForm() {
            if ((this.formData.fBeamcount - 0) >= (this.formData.fMin - 0)) {
                var that = this;
                that.formData.userId = that.userInfo.userId; //当前登录人id
                that.formData.userName = that.userInfo.realName; //当前登录人名称
                if (that.$refs.publicForm.submitForm()) {
                    that.saveForm(that.formData);
                } else {
                    console.log("error");
                }
            } else if ((this.formData.fBeamcount - 0) < (this.formData.fMin - 0)) {
                this.$message.warning("梁板数量不能小于已经预制数");
            } else {
                this.$message.warning("梁板数量请填写数字");
            }
        },
        saveForm(obj) {
            this.isLock = true
            saveBeam(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("保存成功");
                        this.isLock = false
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log(error.msg);
                });
        },
        // 保存
    },
};